package com.unittec.zk.provider.exam.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.exam.service.ElRepoService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.exam.request.*;
import com.unittec.zk.sdk.exam.response.*;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "题库管理")
public class RepoController {
    
    @Resource
    private Log log;
    
    @Resource
    private ElRepoService elRepoService;
    
    //题库管理分页
    @RequestMapping(value = "/201001", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "题库管理分页", notes = "题库管理分页")
    public Result<Res201000> getRepoPage(@RequestBody GatewayRequest<Req201001> req) {
        log.debug(req, "getRepoPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elRepoService.getRepoPage(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201001", JSON.toJSONString(req), "题库管理分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getRepoPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201001", JSON.toJSONString(req), "题库管理分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201001", JSON.toJSONString(req), "题库管理分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //题库-增
    @RequestMapping(value = "/201002", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "题库-增", notes = "题库-增")
    public Result<Res201000> addRepo(@RequestBody GatewayRequest<Req201002> req) {
        log.debug(req, "addRepo {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elRepoService.addRepo(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201002", JSON.toJSONString(req), "题库-增", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addRepo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201002", JSON.toJSONString(req), "题库-增", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201002", JSON.toJSONString(req), "题库-增", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //题库-改
    @RequestMapping(value = "/201003", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "题库-改", notes = "题库-改")
    public Result<Res201000> updateRepo(@RequestBody GatewayRequest<Req201003> req) {
        log.debug(req, "updateRepo {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elRepoService.updateRepo(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201003", JSON.toJSONString(req), "题库-改", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "updateRepo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201003", JSON.toJSONString(req), "题库-改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201003", JSON.toJSONString(req), "题库-改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //题库-查
    @RequestMapping(value = "/201004", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "题库-查", notes = "题库-查")
    public Result<Res201000> getRepoById(@RequestBody GatewayRequest<Req201004> req) {
        log.debug(req, "getRepoById {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elRepoService.getRepoById(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201004", JSON.toJSONString(req), "题库-查", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getRepoById {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201004", JSON.toJSONString(req), "题库-查", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201004", JSON.toJSONString(req), "题库-查", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //题库-删
    @RequestMapping(value = "/201005", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "题库-删", notes = "题库-删")
    public Result<Res201000> delRepo(@RequestBody GatewayRequest<Req201005> req) {
        log.debug(req, "delRepo {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elRepoService.delRepo(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201005", JSON.toJSONString(req), "题库-删", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "delRepo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201005", JSON.toJSONString(req), "题库-删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201005", JSON.toJSONString(req), "题库-删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

}
